/*********************************************************
*Copyright (c) 2009 Rob Groves. All Rights in Pw
*@des: 异步并发TCP Sever
*@name: Async_Server.h
*@author: zhognsx
*@time: 2015-11-16
*********************************************************/

#ifndef __ASYNC_SERVER_H__
#define __ASYNC_SERVER_H__

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>  
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp> 
#include <boost/noncopyable.hpp>
#include <boost/thread.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/pool/object_pool.hpp>
#include <boost/make_shared.hpp>

#include "ios_pool.h"
#include "session_manager.h"

typedef boost::shared_ptr<session_manager> session_manager_ptr;

class async_server : public boost::noncopyable
{
public:
	async_server(short port);
		//async_server(boost::asio::io_service& ios, Read_Msg_CallBack func, short sPort);
	virtual ~async_server(){
		std::cout << "async_server  delete" << std::endl;
	}

	void init(Read_Msg_CallBack func);

	//获取session管理器ptr
	session_manager_ptr get_session_manager();
	io_service_pool& get_servic_pool();
	//
	void run();
	void stop();
	//注册关闭连接回调
	void RegisterCloseConnCallBack(Close_Conn_CallBack ccc);
	//
	void async_accept();
	//
	void handle_accept(boost::shared_ptr<session>& new_session,
		const boost::system::error_code& error);
private:
	io_service_pool io_service_pool_;

	boost::asio::io_service& ios_;
	boost::asio::ip::tcp::acceptor acceptor_;
	//链接管理
	session_manager_ptr p_manager;
	//回调方法
	Read_Msg_CallBack read_func;
	Close_Conn_CallBack close_func;
	//对象内存池 分配session
	boost::shared_ptr<boost::object_pool<session>> session_pool;
};


#endif //__ASYNC_SERVER_H__